#ifndef SERVER_H
#define SERVER_H
#include "thread.h"
#include "tcpsocket.h"
#include <QDebug>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include <event2/bufferevent.h>
#include<event2/listener.h>
#include<event2/event_compat.h>

class Tcpsocket;
class Tcpserver
{
    friend class Tcpsocket;
public:
    Tcpserver(int num=8);
    int listen(int port,const char* ip=NULL);
    //服务器开始
    void start();
protected:
    static void listen_cb(struct evconnlistener* listener,
                          evutil_socket_t fd,
                          struct sockaddr* addr,
                          int socklen,
                          void* ptr);
    void listen_run(evutil_socket_t fd,sockaddr_in* sin);

    virtual  void readEvent(Tcpsocket*s){};
    virtual  void writeEvent(Tcpsocket*s){};
    virtual  void closeEvent(Tcpsocket*s,short what){};
    virtual  void connectEvent(Tcpsocket*s){};
private:
    int threadnum;
    Thread* thread_pool;
    int next_thread;
    event_base *m_base;
    evconnlistener* listener;
};

#endif // SERVER_H
